<?php
class links_admin_broken
{
	var $mydirname;
	var $controller_name;
	var $Links;
	var $constprefMI;
	var $_db_model;

	var $nav_html = NULL;
	var $navinfo = NULL;

	/** Constructor
	 *
	**/
	function links_admin_broken($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();

		$this->constprefMI = '_MI_'. $this->mydirname;
		$this->_db_model = $this->Links->setDbModel(str_replace('admin_', '', $this->controller_name));
	}

	function index($status = 0, $offset = 0)
	{
		$per_page = 20;

		$total_count = $this->_db_model->getTotalCount($status);

		$data = array();
		if ($total_count > 0) {
			$data = $this->_db_model->getBrokens($status, $per_page, $offset);

			if ($total_count > $per_page) {
				$this->_set_navigation($total_count, $per_page, $status, $offset);
			}
		}

		$data = array(
			'brokens'          => $data,
			'totalbrokenlinks' => $total_count,
			'status'           => $status,
			'mod_name'         => htmlspecialchars($this->Links->module_name, ENT_QUOTES, _CHARSET),
			'menu_name'        => htmlspecialchars(constant($this->constprefMI .'_ADMENU3'), ENT_QUOTES, _CHARSET),
			'gticket'          => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_admin_broken'),
			'nav'              => $this->nav_html,
			'navinfo'          => $this->navinfo,
		);

		$template = $this->mydirname . '_admin_broken.html';
		$this->Links->adminView($template, $data);
	}

	function delete()
	{
		$this->Links->basicPostCheck('links_admin_broken', FALSE);
		$delete_lid = ($_POST['delete_lid']) ? intval($_POST['delete_lid']): 0;
		$delete_reportid = ($_POST['delete_reportid']) ? intval($_POST['delete_reportid']): 0;

		$message = _MD_A_LINKS_CAT_DELETED_ERR;
		switch ($_POST['delete_item']) {
			case 'both':
				if (! $link = $this->Links->getDataByLid($delete_lid)) {
					$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=broken', 2, _MD_CANT_GET_DATA);
					exit;
				}
				if ($this->_db_model->deleteLink($delete_lid)) {
					$message = _MD_A_LINKS_CAT_DELETED;
				}
				break;
			case 'report':
				if (! $this->_db_model->getById($delete_reportid)) {
					$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=broken', 2, _MD_CANT_GET_DATA);
					exit;
				}
				if ($this->_db_model->deleteReport($delete_reportid)) {
					$message = _MD_A_LINKS_CAT_DELETED;
				}
				break;
			default:
				break;
		}

		$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=broken', 2, $message);
	}


	function change_status($reportid = 0, $current_page_status = 0)
	{
		$broken = $this->_db_model->getById($reportid);

		if ($broken && $broken['status'] == $current_page_status) {
			if ($this->_db_model->_status_change($reportid, ($current_page_status+1)%2) ){
				$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=broken&amp;s='. ($current_page_status+1)%2 , 2, _MD_A_STATUS_CHANGE_OK);
				exit;
			} else {
				$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=broken&amp;s='. $current_page_status, 2, _MD_A_STATUS_CHANGE_NG);
				exit;
			}
		} else {
			$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=broken&amp;s='. $current_page_status, 2, _MD_CANT_GET_DATA);
		}
	}

	function _set_navigation($numrows, $per_page, $status = 0, $offset = 0)
	{
		$extra_arg = array(
				's' => $status,
				'p' => $offset,
				'c' => str_replace('admin_', '', $this->controller_name),
				'm' => 'index'
			);
		$options = $this->Links->uri_option_arr;
		$options['url_approach'] = 0;
		$options['admin'] = TRUE;

		$nav = new links_PageNav($numrows, $per_page, $extra_arg, $options, $this->mydirname);
		$this->nav_html = $nav->renderNav(5) ;

		$last = $offset + $per_page ;
		if( $last > $numrows ) $last = $numrows ;
		$this->navinfo = sprintf( _MD_LINKS_NAVINFO , $offset + 1 , $last , $numrows ) ;
	}

}
